package com.iot.north.app.demo.utils;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;

/**
 * json工具类
 * */
public class JsonUtil {
  private static ObjectMapper objectMapper = new ObjectMapper();

  static {
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  }

  @SneakyThrows
  public static JsonNode parse(String str) {
    return objectMapper.readValue(str, JsonNode.class);
  }

  @SneakyThrows
  public static String toJson(Object obj) {
    return objectMapper.writeValueAsString(obj);
  }

  @SneakyThrows
  public static byte[] toBytes(Object obj) {
    return objectMapper.writeValueAsBytes(obj);
  }

  @SneakyThrows
  public static <T> T fromJson(String payload, Class<T> tClass) {
    return objectMapper.readValue(payload, tClass);
  }

  public static <T> T convert(Object obj, Class<T> clazz) {
    return objectMapper.convertValue(obj, clazz);
  }
}
